import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tiktokadult/hj_utils/api_service/mine_service.dart';
import 'package:tiktokadult/tools_base/debug_log.dart';

import '../../../hj_model/mine/credit_record_model.dart';

class IntegralRecordLogic extends GetxController {
  List<CreditRecordModel> groupList = [];
  bool isLoadingHistoryData = true;
  RefreshController refreshController = RefreshController();
  int currentPage = 1;

  IntegralRecordLogic();

  @override
  onReady() {
    super.onReady();
    loadData();
  }

  void loadData({int page = 1}) async {
    try {
      final res = await MineService.getCreditRecords(pageNumber: page, pageSize: 20);
      if (res.isNotEmpty) {
        if (page == 1) groupList.clear();
        currentPage = page;
        groupList.addAll(res ?? []);
      }
      refreshController.refreshCompleted();
      (res.length ?? 0) < 20 ? refreshController.loadComplete() : refreshController.loadNoData();
    } catch (e) {
      refreshController.refreshCompleted();
      refreshController.loadComplete();
      debugLog(e);
    }
    update();
    isLoadingHistoryData = false;
  }

  loadMoreData() => loadData(page: currentPage + 1);
}
